<?php
/**
 * The Url class
 *
 * @todo more url pattern
 */
class Url
{
	/**
	 * The url pattern
	 * 
	 * @var string 
	 */
	static protected $urlPattern;

	/**
	 * Generate url string
	 * 
	 * @param string $module
	 * @param string $action
	 * @param array $args 
	 * @param array $options 
	 * @example generate('adress', 'set', array('id' => 1), array('pattern' => 'rewrite', 'url_encoded' => false))
	 */
	static public function generate($module, $action, $args = null, $options = null)
	{
		$url = '';
		$pattern = isset($options['pattern']) ? $options['pattern'] : self::$urlPattern;
		switch ($pattern)
		{
			case 'standard':
			default:// URL format: ?module=module_name&action=action_name&key=value
				$url .= "?module=$module&action=$action";
				if ($args)
				{
					foreach($args as $key => $value)
					{
						if (isset($options['url_encoded']) && true == $options['url_encoded'])
						{
							$value = urlencode($value);
						}
						$url .= "&$key=$value";
					}
				}
				break;
			case 'rewrite':// URL formar: controlloer_name/action_name?key=value
				$url .= "$module/$action";
				if ($args)
				{
					$queryString = '';
					foreach($args as $key => $value)
					{
						if (isset($options['url_encoded']) && true == $options['url_encoded'])
						{
							$value = urlencode($value);
						}
						$queryString .= "&$key=$value";
					}
					$url .= '?' . substr($queryString, 1);
				}
				break;
			case 'path_info':// URL formar: controlloer_name/action_name/key/value
				$url .= "$module/$action";
				if ($args)
				{
					foreach($args as $key => $value)
					{
						if (isset($options['url_encoded']) && true == $options['url_encoded'])
						{
							$value = urlencode($value);
						}
						$url .= "/$key/$value";
					}
				}
				break;
		}
		return $url;
	}

	/**
	 * Set default pattern
	 * 
	 * @param string $pattern 
	 * @return void 
	 */
	static public function setDefaultPattern($pattern)
	{
		self::$urlPattern = $pattern;
	}
}
